//bench.js
const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
    equipTypeArray: [{ value: 1, name: '轮椅', checked: true }, { value: 2, name: '担架'}],
    imgArray: []
  },
  addImg: function (e) {
    let that = this;
    wx.chooseImage({
      count: 6,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        for (let i = 0; i < res.tempFilePaths.length; i++) {
          that.data.imgArray.push(res.tempFilePaths[i])
        }
        that.setData({
          imgArray: that.data.imgArray
        })
        console.log(that.data.imgArray)
      }
    })
  },
  deleteImg: function (e) {
    let that = this
    let deleteIndex = e.currentTarget.dataset.index
    that.data.imgArray.splice(deleteIndex, 1)
    that.setData({
      imgArray: that.data.imgArray
    })
  },
  formSubmit: function (e) {
    let that = this;
    if (!e.detail.value.equipCode){
      that.toast.showToast('请输入设备编号')
      return
    }
    let formData = e.detail.value
    formData.addEquipDate = new Date()
    formData.imgArrar = that.data.imgArray
    console.log(formData)
    // 这里提交添加设备的日志 
    wx.request({
      url: util.CONST_DATA.BASE_URL + 'inspection/addPileAndEquipment',
      header: app.globalData.header,
      method: 'POST',
      data: {
        hospitalId: parseInt(that.data.hospitalId),
        pileCode: parseInt(formData.pileCode.replace(/[^0-9]/ig,"")),
        pileLocation: that.data.location,
        equipCode: parseInt(formData.equipCode.replace(/[^0-9]/ig,"")),
        equipType: formData.equipType,
        imgArray: that.data.imgArray,
        lat: parseFloat(that.data.latitude),
        lng: parseFloat(that.data.longitude)
      },
      success(res){
        console.log('接口11>>',res)
        if(res.data==0){
          wx.navigateBack({
            delta: 1
          })
        }
      }
    })
  },
  onLoad: function (options) {
    let that = this
    that.setData({
      pileCode: options.pileCode,
      hospitalName: options.hospitalName,
      hospitalId: options.hospitalId,
      location: options.location
    })
  },
  onReady: function () {
    this.toast = this.selectComponent("#toast")
    // Do something when page ready.
  },
  onShow: function () {
    // Do something when page show.
  },
  onHide: function () {
    // Do something when page hide.
  },
  onUnload: function () {
    // Do something when page close.
  },
  onPullDownRefresh: function () {
    // Do something when pull down
  },
  // Event handler.
  viewTap: function () {
    this.setData({
      text: 'Set some data for updating view.'
    })
  }
})